#include "Game.h"


Game::Game()
{

}

void Game::init()
{
	m_app.create(sf::VideoMode(460, 460, 32), "War's Body", sf::Style::None | sf::Style::Close);
	m_app.setFramerateLimit(60);
	m_view = m_app.getDefaultView();

	m_imageManager.setImage("ressource/ressources.png", 16, sf::Color::Color(255, 81, 243));

	m_player.init(&m_imageManager);

	m_level.init(&m_imageManager);
	m_level.loadLevel("ressource/Level", &m_player);

	m_hud.init(&m_imageManager);

	m_gameMenu.init(&m_imageManager);

	m_pause = false;

	m_font.loadFromFile("ressource/font.ttf");
	m_win.setString("Win !");
	m_win.setFont(m_font);
	m_win.setCharacterSize(96);

}

void Game::render()
{
	while(m_app.isOpen())
	{
		while(m_app.pollEvent(m_event))
		{
			if(m_event.type == sf::Event::Closed)
				m_app.close();
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::F1))
            {
                m_screen = m_app.capture();
                m_screen.saveToFile("screen.png");
            }
            if(m_event.type == sf::Event::GainedFocus)
                m_pause = false;
            if(m_event.type == sf::Event::LostFocus)
                m_pause = true;

		}
		m_app.setView(m_view);
		m_app.clear(sf::Color::Black);
		if(m_gameMenu.getActive())
		{
            m_level.render(&m_app, &m_player);
            m_gameMenu.render(&m_app);
            m_gameMenu.event();
		}
		else
		{
		    if(m_pause)
		    {
                m_level.render(&m_app, &m_player);
                m_hud.render(&m_app, &m_player, &m_view);
		    }
		    else if(m_level.getFinishLevel())
		    {
                m_level.render(&m_app, &m_player);
                m_hud.render(&m_app, &m_player, &m_view);
                m_win.setColor(sf::Color::Color(67, 67, 67));
                m_win.setPosition(m_view.getCenter().x - m_win.getGlobalBounds().width / 2 + 2, m_view.getCenter().y - m_win.getGlobalBounds().height + 2);
                m_app.draw(m_win);
                m_win.setColor(sf::Color::Red);
                m_win.setPosition(m_view.getCenter().x - m_win.getGlobalBounds().width / 2, m_view.getCenter().y - m_win.getGlobalBounds().height);
                m_app.draw(m_win);
		    }
            else
            {
                m_player.event(&m_level, &m_view);
                m_level.render(&m_app, &m_player);
                m_level.majEntity(&m_player);
                m_hud.render(&m_app, &m_player, &m_view);
            }
		}
		m_app.display();
	}
}
